export const LocalUserKey = "user-info";
export const CookieAuthTokenKey = "user-token";

export const useUserStore = defineStore("user", {
  state: (): UserInfo => {
    return {
      id: null,
      token: null,
      avatar: "",
      username: "",
      nickname: "",
      roles: [],
      menus: [],
      permissions: [],
      menuTree: [],
    } as UserInfo;
  },
  getters: {
    adminSystemList: (state) => {
      return getAdminSystemList(state.menuTree);
    },
  },
  actions: {
    setUser(user: UserInfo) {
      this.id = user.id;
      this.username = user.username;
      this.nickname = user.nickname;
      this.avatar = user.avatar;
      this.roles = user.roles;
      this.menus = user.menus;
      this.permissions = user.permissions;
    },
    setToken(token: string) {
      this.token = token;
    },
    clearUser() {
      this.id = null;
      this.token = null;
      this.username = "";
      this.nickname = "";
      this.avatar = "";
      this.roles = [];
      this.menus = [];
      this.permissions = [];
      this.menuTree = [];
    },
    setMenuTree(menuTree: Menu[]) {
      this.menuTree = menuTree;
    },
  },
  persist: {
    storage: piniaPluginPersistedstate.localStorage(),
  },
});
